package model;

import java.sql.*;

public class DBCreatetion {
	public static Connection getConnection() {
		String url = "jdbc:mysql://localhost:3306/";
		String dbName = "cnpm";
		String driver = "com.mysql.jdbc.Driver";
		String userName = "root";
		String password = "123456";
		Connection conn = null;
		try {
			Class.forName(driver).newInstance();
			conn = DriverManager
					.getConnection(url + dbName, userName, password);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return conn;
	}

	public static void createDB(Connection conn) {
		Statement st;
		try {
			st = conn.createStatement();
//			st.executeUpdate("CREATE TABLE `cnpm`.`food` (`id` INT NOT NULL AUTO_INCREMENT,`name` VARCHAR(45) NOT NULL,`image` VARCHAR(500) NOT NULL,`type` VARCHAR(45) NOT NULL,`source` VARCHAR(45) NOT NULL,`dateOfPurchase` DATE NOT NULL,`wholesalePrices` DOUBLE NOT NULL,`retailPrice` DOUBLE NOT NULL,`status` VARCHAR(45) NOT NULL,`info` VARCHAR(500) NOT NULL,`qr` VARCHAR(45) NOT NULL,PRIMARY KEY (`id`),UNIQUE INDEX `name_UNIQUE` (`name` ASC));");
			st.executeUpdate("CREATE TABLE `cnpm`.`user` (`id` INT NOT NULL AUTO_INCREMENT,`name` VARCHAR(45) NOT NULL,`password` VARCHAR(45) NOT NULL,`role` VARCHAR(45) NOT NULL,PRIMARY KEY (`id`),UNIQUE INDEX `name_UNIQUE` (`name` ASC));");
			conn.close();
			System.out.println("Create table done!");
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		try {
			Connection conn = getConnection();
			createDB(conn);
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
